<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitInfoCtl;
use Srv\Apps\Proto\DTO\Skit\SkitInfoExtEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitInfoListDTO;
use Srv\Apps\Proto\DTO\Skit\SkitInfoAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitInfoEditDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitInfoListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitInfoAct extends ActionAbstract
{
    /**
     * @url /skit/info/list
     * @auth skitInfoList
     * @req Srv.Apps.Proto.DTO.Skit.SkitInfoListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitInfoListVO
     * 短剧信息列表
     */
    public function getSkitInfoList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitInfoListDTO) || !($VOClass instanceof SkitInfoListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitInfoCtl::tag()->getSkitInfoList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/info/add
     * @auth skitInfoAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitInfoAddDTO
     * 短剧信息新增
     */
    public function skitInfoAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitInfoAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $skioId     = SkitInfoCtl::tag()->skitInfoAdd($setData);
        if($skioId < 1) {
            $errMsg = '短剧信息新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_INFO_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '短剧信息新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_INFO_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/info/edit
     * @auth skitInfoEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitInfoEditDTO
     * 短剧信息编辑
     */
    public function skitInfoEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitInfoEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $skioId         = intval($DTOClass->getSkioId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $SkitInfoCtl    = SkitInfoCtl::tag();
        $LogCtl         = LogCtl::tag();
        $infoSrc        = $SkitInfoCtl->getSkitInfoSrc($skioId);
        if(!isset($infoSrc['skio_id'])){
            $logData    = $setData;
            $errMsg     = '短剧信息不存在';
            $LogCtl->logAdd(LogCtl::SKIT_INFO_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $SkitInfoCtl->skitInfoEdit($skioId, $setData);
        $logData        = ['new' => $setData, 'old' => $infoSrc];
        if(!$result){
            $errMsg     = '短剧信息编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_INFO_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '短剧信息编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_INFO_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/info/ext/edit
     * @auth skitInfoExtEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitInfoExtEditDTO
     * 短剧扩展数据编辑
     */
    public function skitInfoExtEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitInfoExtEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitInfoCtl    = SkitInfoCtl::tag();
        $oldData        = [];
        $newData        = $setData[SkitInfoExtEditDTO::_extItemList];
        if(!$SkitInfoCtl->checkExtData($newData, $oldData)){
            $logData    = $setData;
            $errMsg     = '数据检查错误';
            $LogCtl->logAdd(LogCtl::SKIT_INFO_EXT_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_ERROR, Lang::getString(LangKeyEnum::_LANG_ERROR));
        }
        $logData        = ['new' => $setData, 'old' => $oldData];
        if(!$SkitInfoCtl->skitInfoExtEdit($newData)){
            $errMsg     = '短剧扩展数据编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_INFO_EXT_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '短剧扩展数据编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_INFO_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}